home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
dte5_1.zip
/
WINDOW.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-06
|
20KB
|
663 lines
/*
* Written by Douglas Thomson (1989/1990)
*
* This source code is released into the public domain.
*/
/*
* Name: dte - Doug's Text Editor program - window module
* Purpose: This file contains the code associated with opening and sizing
* windows, and also displaying the help window.
* File: window.c
* Author: Douglas Thomson
* System: this file is intended to be system-independent
* Date: October 12, 1989
*/
#ifdef HPXL
#include "commonh"
#include "utilsh"
#include "windowh"
#else
#include "common.h"
#include "utils.h"
#include "window.h"
#endif
/*
* prototypes for all functions in this file
*/
int open_window ARGS((windows *window, char *name));
void new_window ARGS((char *auto_name, windows *window));
void choose_window ARGS((char *name, windows *window));
void size_window ARGS((windows *window));
void get_help ARGS((windows *window));
void finish ARGS((windows *window));
int create_window ARGS((windows *prev, int top, int bottom, text_ptr cursor,
file_infos *file));
int create_file ARGS((file_infos *prev));
/*
* Name: open_window
* Purpose: To open a new window, and load a file into it.
* Date: October 10, 1989
* Passed: window: information allowing access to the current window
* name: name of file to read
* Returns: OK if window opened successfully
* ERROR if anything went wrong
* Notes: If window is NULL, then this is the first window, and should
* take up the entire screen.
* If window is not NULL, then the new window should start on the
* line below the cursor line of the current window, and continue
* down to what used to be the bottom of the current window. If
* this does not make enough room for the new window, then an
* error is reported, and no new window is created.
*/
int open_window(window, name)
windows *window;
char *name;
{
int existing; /* did the file already exist? */
file_infos *file; /* file structure for file belonging to new window */
int ccol; /* cursor column in new window */
text_ptr cursor; /* start of cursor line in new window */
windows *wp; /* used for scanning windows for same file name */
int file_attrib; /* rwx bits for new file */
if (window) {
/*
* check that there is room for the window (no need if this is the
* first window)
*/
if (window->bottom_line - window->cline < 2 ||
g_display.nlines-3 - window->cline < 2) {
error(WARNING, "move cursor up first");
return ERROR;
}
}
/*
* look for a window whose file has the same name. Check the
* current window (if any) first.
* If there is already a window open on the same file, then the
* new window can share the existing file structure, and the new
* window's cursor should be placed in the same spot in the file.
*/
if (window && strcmp(name, window->file_info->file_name) == 0) {
file = window->file_info;
existing = TRUE;
cursor = window->cursor;
ccol = window->ccol;
/*
* One line is taken by the status line, and the cursor line
* is duplicated. All the other lines can be kept.
*/
window_scroll_down(window->cline+1, window->bottom_line);
window_scroll_down(window->cline+2, window->bottom_line);
}
else {
/*
* Not same file as current window, so try other windows
*/
file = NULL;
for (wp=g_status.window_list; wp; wp = wp->next) {
if (strcmp(name, wp->file_info->file_name) == 0) {
file = wp->file_info;
break;
}
}
if (file) {
/*
* file was already open somewhere
*/
existing = TRUE;
cursor = wp->cursor;
ccol = wp->ccol;
}
else {
/*
* file was not open, so see if it exists on disk
*/
file_attrib = hw_fattrib(name);
existing = file_attrib != ERROR;
if (existing) {
if (load_file(name, FALSE) != OK) {
/*
* The file existed, but was not a plain text file
* or we ran out of memory. In any case, give up
* on the new window.
*/
return ERROR;
}
}
else {
/*
* make sure this gets set properly even if there is no file!
*/
g_status.temp_end = g_status.end_mem;
}
/*
* allocate a file structure for the new file
*/
if (create_file(window ? window->file_info : NULL) == ERROR) {
error(WARNING, "out of memory");
return ERROR;
}
file = window ? window->file_info->next : g_status.file_list;
/*
* set up all the info we need to know about a file, and
* record that we have used some more memory.
*/
strcpy(file->file_name, name);
file->file_attrib = file_attrib;
cursor = g_status.end_mem;
ccol = 0;
file->start_text = g_status.end_mem;
*g_status.temp_end = '\0';
g_status.end_mem = g_status.temp_end + 1;
file->end_text = g_status.end_mem;
/*
* file has not been modified yet
*/
g_status.unsaved = FALSE;
}
}
/*
* Now that we have the file, allocate a window to view the file
*/
if (create_window(window, window ? window->cline+1 : 0,
window ? window->bottom_line : g_display.nlines-1, cursor,
file) == ERROR) {
error(WARNING, "out of memory");
/*
* This is a real nuisance. We had room for the file and the
* file structure, but not enough for the window as well.
* Now we must free all the memory that has already been
* allocated.
*/
if (file->ref_count == 0) {
if (file->prev) {
file->prev->next = file->next;
}
else {
g_status.file_list = file->next;
}
if (file->next) {
file->next->prev = file->prev;
}
g_status.end_mem = file->start_text;
free(file);
}
return ERROR;
}
if (window) {
/*
* record that the current window has lost some lines from
* the bottom for the new window, and adjust its page size
* etc accordingly.
*/
window->bottom_line = window->cline;
setup_window(window);
/*
* we have now finished with the old window, and can concentrate
* on the new one.
*/
window = window->next;
}
else {
window = g_status.window_list;
}
/*
* tell the user if a new file has been created, in case this was not
* intentional.
* Eventually, the user should be given a chance to change the name
* of the file and try again.
*/
if (!existing) {
error(DIAG, "'%s' is a new file", name);
file->new_file = TRUE;
}
/*
* set up the new cursor position as appropriate
*/
window->cursor = cursor;
window->ccol = ccol;
/*
* the new window becomes the current window.
*/
g_status.current_window = window;
return OK;
}
/*
* Name: new_window
* Purpose: To open a new window, and ask the user which file to load into it.
* Date: October 10, 1989
* Passed: auto_name: name of file (NULL if we should pr